/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

function getKthFromEnd(head: ListNode | null, k: number): ListNode | null {
    let prev = head;
    let i = 0;
    while (i < k && prev) {
        prev = prev.next;
        i++;
    }

    let res = head;
    while (prev) {
        res = res.next
        prev = prev.next;
    }
    return res;
};